<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>函数式组件使用props</title>
  </head>
  <body>
    <div id="test1"></div>
    <div id="test2"></div>
    <div id="test3"></div>
    <!-- 引入react核心库  优先 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <!-- 引入react-dom用于支持react操作DOM，依赖react核心库 -->
    <script
      type="text/javascript"
      src="../js/react-dom.development.js"
    ></script>
    <!-- 引入babel，用于将jsx转换为js -->
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <!-- 引入prop-types，用于对组件标签属性进行限制 -->
    <script type="text/javascript" src="../js/prop-types.js"></script>

    <script type="text/babel">
      // class Person extends React.Component {
      //   static propTypes = {
      //     name: PropTypes.string.isRequired, //限制name必须为字符串
      //     age: PropTypes.number, //限制age必须为数字
      //     speak: PropTypes.func, //限制speak必须为函数  函数 用func
      //   };

      //   static defaultProps = {
      //     age: 100, //默认值
      //   };

      //   render() {
      //     const { name, age } = this.props;
      //     return (
      //       <ul>
      //         <li>{name}</li>
      //         <li>{age + 1}</li>
      //       </ul>
      //     );
      //   }
      // }

      function Person(props) {
        const { name, age } = props;
        return (
          <ul>
            <li>{name}</li>
            <li>{age}</li>
          </ul>
        );
      }
      Person.propTypes = {
        name: PropTypes.string.isRequired, //限制name必须为字符串
        age: PropTypes.number, //限制age必须为数字
        speak: PropTypes.func, //限制speak必须为函数  函数 用func
      };

      Person.defaultProps = {
        age: 100, //默认值
      };
      const p = { name: "lisa" };
      ReactDOM.render(
        <Person name={p.name} age={p.age} />,
        document.getElementById("test1")
      );

      ReactDOM.render(
        <Person name="Tom" age={29} />,
        document.getElementById("test2")
      );
      const p1 = { name: "sunliang", age: 29 };
      ReactDOM.render(
        <Person {...p1} speak={speak} />,
        document.getElementById("test3")
      );
      function speak() {
        console.log("speak,我要说话了");
      }
    </script>
  </body>
</html>
